package com.erp.programmer.controller;


import com.alibaba.fastjson.JSON;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dto.LoginUserDto;
import com.erp.programmer.dto.PageDto;
import com.erp.programmer.dto.PermissionDto;
import com.erp.programmer.dto.ResponseDto;
import com.erp.programmer.service.IPermissionService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陈鑫
 * @since 2021-05-23
 */
@RestController
@RequestMapping("/permission")
@CrossOrigin
public class PermissionController {
    @Autowired
    IPermissionService iPermissionService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询权限信息
     * @return
     */
    @PostMapping("/get_data")
    public ResponseDto<PageDto<PermissionDto>> getPermission(@RequestBody PageDto<PermissionDto> pageDto) {
        if (pageDto==null) {
            ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        return iPermissionService.getPermission(pageDto);
    }

    @PostMapping("/save")
    public ResponseDto<PageDto<PermissionDto>> savePermission(@RequestBody PermissionDto permissionDto) {
        if (permissionDto==null) {
            return  ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        //进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(permissionDto);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDto.errorByMsg(validate);
        }
        // 插入
        if (CommonUtil.isEmpty(permissionDto.getId())) {
            // add
            return iPermissionService.insertPermission(permissionDto);
        } else {
            // edit
            return iPermissionService.updatePermission(permissionDto);
        }
    }

    /**
     * 删除权限信息
     * @param permissionDto
     * @return
     */
    @PostMapping("/delete")
    public ResponseDto<PageDto<PermissionDto>> deletePermission(@RequestBody PermissionDto permissionDto) {
        if (permissionDto==null || CommonUtil.isEmpty(permissionDto.getId())) {
            return  ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        if (!iPermissionService.removeById(permissionDto.getId())) {
            return ResponseDto.errorByMsg(CodeMsg.AUTHORITY_DELETE_ERROR);
        }
        return iPermissionService.deletePermission(permissionDto);
    }

    @PostMapping("/get_permission")
    public ResponseDto<List<PermissionDto>> getPermissionByToken(@RequestBody LoginUserDto loginUserDto) {
        if (CommonUtil.isEmpty(loginUserDto.getToken())) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        String stringValue = stringRedisTemplate.opsForValue().get(loginUserDto.getToken());
        if (stringValue == null) {
            return ResponseDto.errorByMsg(CodeMsg.TOKEN_EXPIRED_OR_NOT_EXIST);
        }
        LoginUserDto userDto = JSON.parseObject(stringValue, LoginUserDto.class);
        return iPermissionService.getPermissionByToken(userDto.getRoleId());
    }

}

